/*
 * Project Name: cms_web
 * File Name: GoodsItemKeywordsAction.java
 * Copyright: ZKTeco Corporation 2015
 * Description: TODO 请在此处添加对本文件的描述
 * Creator: Administrator
 * Create Date: 2015年5月5日 下午5:10:33
 */
package com.shishuo.cms.action.manage;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.GoodsItemKeywordsEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.GoodsItemKeywordsService;

@Controller
@RequestMapping("/admin/goodsKeywords")
public class GoodsItemKeywordsAction extends AdminBaseAction {
	@Autowired
	private GoodsItemKeywordsService goodsItemKeywordsService;
	
	
	
	@InitBinder("goodsItemKeywordsEntity")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("goodsItemKeywordsEntity.");
	}
	
	@AuthPassport
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String add(ModelMap modelMap)
	{
		modelMap.put("goodsItemKeywordsEntity", new GoodsItemKeywordsEntity());
		return "system/goodsKeywords/add";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/addNew.json", method = RequestMethod.POST)
	public JsonVo<String> addNewGoodsItemKeywordsEntity(@ModelAttribute GoodsItemKeywordsEntity goodsItemKeywordsEntity) throws Exception
	{
		JsonVo<String> json = new JsonVo<String>();
		//验证填写的是否正确
		if (goodsItemKeywordsEntity.getGoodsKeywords().equals("")) {
			json.getErrors().put("goodsKeywords", this.getMsgByMsgKey("system.classesNameNotEmpty"));
		}
		if (goodsItemKeywordsEntity.getGoodsKeywordsAlias().equals("")) {
			json.getErrors().put("goodsKeywordsAlias", this.getMsgByMsgKey("system.classesNameNotEmpty"));
		}
		if (validateJsonIsError(json)) return json;
		goodsItemKeywordsService.insert(goodsItemKeywordsEntity);
		json.setResult(true);
		return json;
	}
	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) throws Exception {
		modelMap.put("pageVo", goodsItemKeywordsService.getAllListPage(pageNum,10));
		return "system/goodsKeywords/all";
	}

	
	@AuthPassport
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(
			@RequestParam(value = "id", defaultValue = "0") long id,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		GoodsItemKeywordsEntity goodsItemKeywordsEntity=goodsItemKeywordsService.getById(id);
		modelMap.put("goodsItemKeywordsEntity", goodsItemKeywordsEntity);
		return "system/goodsKeywords/update";
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.GET)
	public JsonVo<String> deleteClasses(
			@RequestParam(value = "id") long id) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		goodsItemKeywordsService.deleteById(id);
		json.setMsg("OK");
		json.setResult(true);
		return json;
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> editClasses(@ModelAttribute GoodsItemKeywordsEntity goodsItemKeywordsEntity)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		GoodsItemKeywordsEntity goodsItemKeywordsEntityOld = goodsItemKeywordsService.getById(goodsItemKeywordsEntity.getGoodsKeywordsId());
		goodsItemKeywordsEntityOld.setGoodsKeywords(goodsItemKeywordsEntity.getGoodsKeywords());
		goodsItemKeywordsEntityOld.setGoodsKeywordsAlias(goodsItemKeywordsEntity.getGoodsKeywordsAlias());
		goodsItemKeywordsService.update(goodsItemKeywordsEntityOld);
		json.setMsg("OK");
		json.setResult(true);
		return json;
	}
}
